package com.cn.wanxi.control;/**
 * @BelongsProject: online
 * @BelongsPackage: com.cn.wanxi.control
 * @Author: lld
 * @CreateTime: 2020-03-05 19:04
 * @Description: control
 */

import com.cn.wanxi.entity.SysSelectEntity;
import com.cn.wanxi.entity.TestPaperEntity;
import com.cn.wanxi.service.TestPaperService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TestPaperController
 * @Author lld
 * @Date 2020/3/5 19:04
 * @Version 1.0
 **/
@Controller
@RequestMapping("/testPaper")
public class TestPaperController {
    @Autowired
    private TestPaperService testPaperService;
    JSONObject jsondata = new JSONObject();
    ArrayList<Integer> titleList = new ArrayList();
    int i = 0;
    @RequestMapping("/delete")
    @ResponseBody
    public void del(HttpServletRequest request,HttpServletResponse response ,TestPaperEntity testPaperEntity) throws IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        int result = testPaperService.delete(testPaperEntity);
        String mesage = result == 1 ? "删除成功" : "删除失败";
        JSONObject json = new JSONObject();
        json.put("result",mesage);
        response.getWriter().write(json.toString());
    }
    @RequestMapping("/addTitleId")
    @ResponseBody
    public void addTitleId(HttpServletResponse response, HttpServletRequest request) throws IOException {
        i++;
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        System.out.println("进来");
        String[] titleIds = request.getParameterValues("titleId");
        for (String titleId : titleIds) {
            titleList.add(Integer.parseInt(titleId));
        }
        String mesage = titleIds.length > 0 ? "添加成功":"添加失败";
        jsondata.put("result",mesage);
        jsondata.put("data_length",titleIds.length);
        jsondata.put("titleids"+i,titleIds);

    }

    @RequestMapping("/returnData")
    @ResponseBody
    public void dataReturn(HttpServletRequest request,HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        System.out.println(jsondata.toString());
        response.getWriter().write(jsondata.toString());
    }

    @RequestMapping("/getTitleId")
    @ResponseBody
    public void add(HttpServletRequest request, HttpServletResponse response,TestPaperEntity testPaperEntity) throws Exception {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        for (Integer integer : titleList) {
            System.out.println("title"+integer);
        }
        int result_add = testPaperService.add(testPaperEntity);
        int id = testPaperService.getId(testPaperEntity);
        System.out.println("id:"+id);
        String message = result_add == 1 ? "新增成功" : "新增失败";
        System.out.println(message);
        /*JSONArray jsonArray = JSONArray.fromObject(message);
        response.getWriter().println(jsonArray.toString());*/
        JSONObject json = new JSONObject();
        json.put("result", message);
        json.put("id",id);
//        json.put("titleList",titleList);
        response.getWriter().write(json.toString());
    }

    @RequestMapping("/findAllTest")
    @ResponseBody
    public void findAll(HttpServletResponse response, HttpServletRequest request,TestPaperEntity testPaperEntity) throws IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        List<TestPaperEntity> list = testPaperService.findAll(testPaperEntity);
        /*for (TestPaperEntity entity : list) {
            long test_start_time = entity.getTest_start_time();
            Date data = new Date(test_start_time);
            String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(data);
            entity.setTest_start_time(startTime);
        }*/
        JSONObject json = new JSONObject();
        json.put("result", list);
        response.getWriter().write(json.toString());
    }

    @RequestMapping("/getById")
    @ResponseBody
    public void getById(HttpServletRequest request, HttpServletResponse response,TestPaperEntity testPaperEntity) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        TestPaperEntity list = testPaperService.getById(testPaperEntity);
        JSONObject json = new JSONObject();
        json.put("result", list);
        response.getWriter().write(json.toString());
    }

    @RequestMapping("/updateTestPaper")
    @ResponseBody
    public void update(HttpServletResponse response, HttpServletRequest request,TestPaperEntity testPaperEntity) throws IOException, ParseException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        int result_add = testPaperService.update(testPaperEntity);
        String message = result_add == 1 ? "修改成功" : "修改失败";
        System.out.println(message);
        JSONObject json = new JSONObject();
        json.put("result", message);
        response.getWriter().write(json.toString());
    }
    @RequestMapping("/getByModel")
    @ResponseBody
    public void getByModels(HttpServletRequest request, HttpServletResponse response, SysSelectEntity sysSelectEntity) throws IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        List<SysSelectEntity> model = testPaperService.getByModel(sysSelectEntity);
        JSONObject json = new JSONObject();
        json.put("result", model);
        response.getWriter().write(json.toString());
    }
}
